package com.company;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class EchoClient {
    private static final BufferedReader KEYBOARD_INPUT = new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) throws Exception {
        Socket socket = new Socket("localhost", 9999);
        Scanner scan = new Scanner(socket.getInputStream());
        scan.useDelimiter("\n");
        PrintStream sender = new PrintStream(socket.getOutputStream());
        boolean flag = true;
        while(flag) {
            String input = EchoClient.getString("请输入要发送的内容").trim();
            sender.println(input);
            if (scan.hasNext()) {
                System.out.println(scan.next().trim());
            }
            if (input.equalsIgnoreCase("bye bye")) {
                flag = false;
            }
        }
        socket.close();
        sender.close();
    }

    public static String getString(String str) throws Exception {
        System.out.println(str);
        String inputStr = KEYBOARD_INPUT.readLine();
        return inputStr;
    }
}
